import {EventEmitter, Injectable} from '@angular/core';
import {Http, RequestOptions} from "@angular/http";
import MTSConstants from "../config/config";
import {Vehicle} from "../model/vehicle";
import {BackCode} from "../model/backCode";
import {CamType} from "../model/camType";
import {Driver} from "../model/driver";

@Injectable()
export class CamManageService {

  servicename:string;
  searchEvent: EventEmitter<string> = new EventEmitter();  //车辆管理列表搜索标志
  searchFreshEvent: EventEmitter<string> = new EventEmitter();  //搜索框刷新标志
  listFreshEvent: EventEmitter<string> = new EventEmitter();   //列表刷新标志

  oidEvent: EventEmitter<string> = new EventEmitter();   //表格选中信息OID

  constructor(private http:Http) { }

  //查询车辆信息(返回值：List集合)
  getVehicle():Promise<Vehicle[]>{
    const url = `${MTSConstants.HOSTUser}vehicles?exclude=3`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getVehicle',error);});
  }

  //查询车辆信息(返回值 分页)
  getVehiclesPageInfo(pageNumber:number,pageSize:number,name:string,capacity:string){
    const url = `${MTSConstants.HOSTUser}vehiclespageinfo?exclude=2,3&pageNumber=${pageNumber}&pageSize=${pageSize}&name=${name}&capacity=${capacity}`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getVehiclesPageInfo',error);});
  }

  //根据OID查询车辆信息
  getVehicleByOID(oid:string){
    const url = `${MTSConstants.HOSTUser}vehicle/${oid}`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getVehicleByOID',error);});
  }

  //新增车辆信息
  addVehicle(postvalue:Vehicle,fileoids:string,imgoid: string):Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}vehicle`;
    postvalue.purchaseDate = new Date(postvalue.purchaseDateToString);
    postvalue.annualCheckDate = new Date(postvalue.annualCheckDateToString);
    const body = postvalue;
    const headers = MTSConstants.headers;//spring的restful接口用这个
    if(headers.has("attachs"))
      headers.delete("attachs");
    if(fileoids==""){
      fileoids = "#";
    }
    if(headers.has("images"))
      headers.delete("images");
    if(imgoid==""){
      imgoid = "#";
    }
    headers.append("attachs",fileoids);
    headers.append("images",imgoid);
    const options = new RequestOptions({ headers: headers });
    return this.http.post(url, body, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('addVehicle',error);});
  }

  //修改车辆信息
  updateVehicle(postvalue:Vehicle,fileoids: string,imgoid: string): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}vehicle`;
    postvalue.purchaseDate = new Date(postvalue.purchaseDateToString);
    postvalue.annualCheckDate = new Date(postvalue.annualCheckDateToString);
    const body = postvalue;
    const headers = MTSConstants.headers;//spring的restful接口用这个
    if(headers.has("attachs"))
      headers.delete("attachs");
    if(fileoids==""){
      fileoids = "#";
    }
    if(headers.has("images"))
      headers.delete("images");
    if(imgoid==""){
      imgoid = "#";
    }
    headers.append("attachs",fileoids);
    headers.append("images",imgoid);
    const options = new RequestOptions({ headers: headers });
    return this.http.put(url, body, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('updateVehicle',error);});
  }

  //删除车辆信息
  deleteVehicle(oid:string): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}vehicle/${oid}`;
    //let body = JSON.stringify(val);//这个也可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    const options = new RequestOptions({ headers: headers });
    return this.http.delete(url, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('deleteVehicle',error);});
  }

  //查询车辆类型信息
  getVehicleTypes():Promise<CamType[]>{
    const url = `${MTSConstants.HOSTUser}vehicletypes`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getVehicleTypes',error);});
  }

  handleError(functionName:string,error: any): Promise<any> {
    console.error(`服务 : ${this.servicename} , 函数 : ${functionName} 。发生错误 : `, error);
    return Promise.reject(error.message || error);
  }

}
